package com.lightsaber.trade.as.web.retail;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.lightsaber.trade.as.entity.reta.Order;
import com.lightsaber.trade.as.entity.reta.OrderItem;
import com.lightsaber.trade.as.entity.reta.OrderTemplate;
import com.lightsaber.trade.as.service.reta.OrderManager;
import com.lightsaber.trade.as.service.reta.OrderTemplateManager;
import com.lightsaber.trade.core.common.util.Calc;
import com.lightsaber.trade.core.web.BaseAction;

@Namespace("/retail")
@ParentPackage("report-default")
@Result(name = "order-report", location = "/report/retail/order-item.jasper", type = "jasper", params = {
        "dataSource", "orderItemList", "reportParameters", "reportParams", "format", "PDF" })
public class OrderAction extends BaseAction {

    @Autowired
    private OrderManager orderManager;
    @Autowired
    private OrderTemplateManager orderTemplateManager;

    @Value(value = "#{appProperties['retail.page.size']}")
    private String PAGE_SIZE;

    private Page<Order> page;
    private boolean clear;
    private Order order;
    private List<OrderItem> orderItemList;
    private Map<String, Object> reportParams = new HashMap<String, Object>();

    @Override
    public String execute() throws Exception {
        if (clear) {
            order = null;
        }

        if (page == null) {// Click 'Search' button first
            page = new Page<Order>(Integer.valueOf(PAGE_SIZE));
        } else {// Click 'Search' button
            page.setPageSize(Integer.valueOf(PAGE_SIZE));
        }

        if (!page.isOrderBySetted()) {
            page.setOrderBy("name");
            page.setOrder(Page.ASC);
        }

        orderManager.findPage(order, page);
        return SUCCESS;
    }

    public String printReport() {
        order = orderManager.get(order.getId());

        reportParams.put("order", order);
        orderItemList = order.getOrderItemList();

        return "order-report";
    }

    public String useTemplate() {
        order = orderManager.useOrderTemplate(order.getId(), order.getOrderTemplate().getId());

        return INPUT;
    }

    public String edit() throws Exception {
        order = orderManager.get(order.getId());

        return INPUT;
    }

    public String delete() throws Exception {
        orderManager.delete(order.getId());

        return execute();
    }

    public String save() throws Exception {
        order = orderManager.saveOrder(order);

        return INPUT;
    }

    public String add() {
        return INPUT;
    }

    public void ajaxGetTotalPrice() {
        order = orderManager.get(order.getId());

        Struts2Utils.renderText(Calc.leaveTwoDigital(order.getTotalPrice()));
    }

    public Page<Order> getPage() {
        return page;
    }

    public void setPage(Page<Order> page) {
        this.page = page;
    }

    public boolean isClear() {
        return clear;
    }

    public void setClear(boolean clear) {
        this.clear = clear;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    // 渲染页面时显示下拉框
    public List<OrderTemplate> getOrderTemplateList() {
        return orderTemplateManager.getAll();
    }

    public List<OrderItem> getOrderItemList() {
        return orderItemList;
    }

    public void setOrderItemList(List<OrderItem> orderItemList) {
        this.orderItemList = orderItemList;
    }

    public Map<String, Object> getReportParams() {
        return reportParams;
    }

    public void setReportParams(Map<String, Object> reportParams) {
        this.reportParams = reportParams;
    }

}
